package com.xy.transfer.controller;

import com.xy.transfer.entity.Account;
import com.xy.transfer.entity.Custom;
import com.xy.transfer.feign.BankICBCFeign;
import com.xy.transfer.feign.TransferFeign;
import com.xy.transfer.service.AccountService;
import com.xy.transfer.service.BankICBCService;
import com.xy.transfer.service.UserService;
import com.xy.transfer.utils.RSAUtils;
import com.xy.transfer.vo.TransferInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SendController {

    @Autowired
    UserService userService;
    @Autowired
    AccountService accountService;
    @Autowired
    TransferFeign transferFeign;
    @Autowired
    BankICBCService bankICBCService;

    @PostMapping("/out/acc")
    public String send(@RequestBody  TransferInfoVO vo) throws Exception {


        Custom custom = userService.getUserInfo(vo.getUsername());
        Account account = accountService.getAccInfo(vo.getAccNo());
        //1.查询用户信息 核对姓名，身份证号，和银行卡号
        if (custom == null){
            return "没有该客户";
        }
        if (!vo.getUserCertNo().equals(custom.getUsercertno())){
            return "身份证不一致";
        }else if(!vo.getAccNo().equals(custom.getAccno())){
            return "银行卡号不一致";
        }
        //2.判断账户状态
        if(account ==null){
            return "没有该账户信息";
        }
        if(!(account.getAccstatus() == 200L)){
            return "账户异常";
        }


        //3判断余额是否足够
        Double in = transferFeign.getInAcc(vo.getAccNo());
        Double out =transferFeign.getOutAcc(vo.getAccNo());
        if(account.getAccfund().doubleValue()+in-out<0){
            return "余额不足";
        }
        String str =bankICBCService.transferAcc(vo);
        return str;
    }
    @PostMapping("/retry/acc")
    public String retryAcc(@RequestBody  TransferInfoVO vo){
        return bankICBCService.retryAcc(vo);
    }
}
